import React, {Component} from 'react';
import './index.less';
import {base} from 'nc-lightapp-front';
import EmptyImg from "src/hrpub/common/components/emptyImg";
import Pagination from 'src/hrpub/common/components/Pagination/index';

class MainTable extends Component {
    constructor(props) {
        super(props);
        this.state = {
            tableHeight: ''
        };
        this.setTableHeight = this.setTableHeight.bind(this);
        this.beforeEventHandle = this.beforeEventHandle.bind(this);
        this.afterEventHandle = this.afterEventHandle.bind(this);
        this.onRowDoubleClick = this.onRowDoubleClick.bind(this);
    }

    componentDidMount() {
        this.setTableHeight();
    }

    setTableHeight() {
        const mainTable = document.getElementsByClassName('main-table');
        if (mainTable[0]) {
            this.setState({
                tableHeight: mainTable[0].clientHeight - 36
            })
        }
    }

    //分页事件
    paginationEve(key) {
        this.props.paginationEve(key)
    }

    // 每页显示条数
    pageSizeSelect(val) {
        this.props.pageSizeSelect(val)
    }

    beforeEventHandle(props, moduleId, item, index, value, record) {
        return record.values.conttype.value === "1";
    }

    afterEventHandle(props, moduleId, key, value, changedrows, index, record) {
    }

    //表单双击事件
    onRowDoubleClick(record) {
        this.props.onRowDoubleClick(record)
    }


    render() {
        const {editTable, orgVal, agreementType, pageInfo, isVisiblePagination, json} = this.props;
        let tableid = (agreementType && agreementType.indexOf('hrcm_agreement') === 0) ? agreementType + 'List' : agreementType;
        return (
            <div className='main-table'>
                    {!orgVal.refpk || !tableid ? 
                        <EmptyImg text={json['cm6011-000143']}/> 
                        :
                        <div calssName='data nc-bill-table-area'>
                            {editTable.createEditTable(tableid, {
                                showIndex: true,
                                showCheck: true,
                                cancelCustomRightMenu: true,
                                height: this.state.tableHeight,
                                onBeforeEvent: this.beforeEventHandle,
                                onAfterEvent: this.afterEventHandle,
                                onRowDoubleClick: this.onRowDoubleClick,
                            })}
                        </div>
                    }
                
                <div className="pagination" style={{display: isVisiblePagination ? '' : 'none'}}>
                    {!orgVal.refpk || !agreementType ? "" : <Pagination
                        total={pageInfo.total}
                        pageSize={pageInfo.pageSize}
                        showQuickJumper={true}
                        showSizeChanger={true}
                        // current = {state.pageInfo.pageIndex}
                        onChange={this.paginationEve.bind(this)}
                        onShowSizeChange={this.pageSizeSelect.bind(this)}
                        current = {pageInfo.pageIndex * 1}
                    />}
                </div>
            </div>
        );
    }
}

export default MainTable;
